package cn.changein.framework.admin.setting.client.controller;

import cn.changein.framework.base.controller.BaseController;
import cn.changein.framework.base.pojo.page.PageInfo;
import cn.changein.framework.kernel.model.response.ResponseData;
import cn.changein.framework.model.setting.client.entity.Client;
import cn.changein.framework.model.setting.client.model.params.ClientParam;
import cn.changein.framework.service.setting.client.ClientService;
import cn.changein.framework.util.ExcelUtil;
import cn.changein.framework.util.ToolUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * 客户控制器
 *

 * @Date 2020-11-02 19:54:54
 * @Copyright: companyName
 * @Version 1.0
 * @Since JDK1.8
 */
@Controller
@RequestMapping("/client")
@Slf4j
public class ClientController extends BaseController {

    private String PREFIX = "/modular/setting/client";

    @Autowired
    private ClientService clientService;

    /**
     * 跳转到主页面
     *

     * @Date 2020-11-02
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/client.html";
    }

    /**
     * 新增页面
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/client_add.html";
    }

    /**
     * 编辑页面
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/client_edit.html";
    }

    /**
     * 新增接口
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(ClientParam clientParam) {
        return this.clientService.add(clientParam);
    }

    /**
     * 编辑接口
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(ClientParam clientParam) {
        return this.clientService.update(clientParam);
    }

    /**
     * 删除接口
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(ClientParam clientParam) {
        return this.clientService.delete(clientParam);
    }

    /**
     * 批量删除
     *
     * @param clientParam
     * @return
     */
    @RequestMapping("/deleteList")
    @ResponseBody
    public ResponseData deleteList(ClientParam clientParam) {
        return this.clientService.deleteList(clientParam);
    }

    /**
     * 查看详情接口
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(ClientParam clientParam) {
        Client detail = this.clientService.getById(clientParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *

     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/list")
    public PageInfo list(ClientParam clientParam) {
        return this.clientService.findPageBySpec(clientParam);
    }

    /**
     * 禁用/启用
     *

     * @Date 2020-10-29
     */
    @RequestMapping("/setState")
    @ResponseBody
    public ResponseData updateState(ClientParam clientParam) {
        ResponseData responseData = this.clientService.updateState(clientParam);
        return responseData;
    }

    /**
     * 审核与反审核
     *

     * @Date 2020-10-28
     */
    @RequestMapping("/setAudit")
    @ResponseBody
    public ResponseData updateAudit(ClientParam clientParam) {
        ResponseData responseData = this.clientService.updateAudit(clientParam);
        return responseData;
    }

    /**
     * 导出客户Excel表格
     *
     * @param clientParam
     */
    @GetMapping("/exportExcel")
    public void exportBusinessUnit(ClientParam clientParam, HttpServletResponse response) {
        this.clientService.exportBusinessUnit(clientParam, response);
    }

    /**
     * 导入
     *

     * @Date 2020-11-03
     */
    @RequestMapping("/importExcel")
    @ResponseBody
    public ResponseData importExcel(HttpServletRequest request, @Param("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ResponseData.error("文件不能为空");
        }
        String originalFilename = file.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));
        if (suffix.equalsIgnoreCase(".xlsx") || suffix.equalsIgnoreCase(".xls")) {
            return this.clientService.importExcel(file);
        }
        return ResponseData.error("请上传Excel文件");
    }

    /**
     * 模板下载
     *

     * @Date 2020-11-03
     */
    @RequestMapping("/downloadExcelTemplate")
    @ResponseBody
    public void downloadExcelTemplate(HttpServletResponse response) {
        try {
            String path = ToolUtil.getTemplatePath("客户导入模板.xlsx");
            ExcelUtil.downLoadExcel(path, response);

        } catch (Exception e) {
            log.error("客户导入模板下载异常", e);
        }
    }

    /**
     * 跳转到选择客户列表界面
     *

     * @Date 2020-11-02
     */
    @RequestMapping("/clientGrid")
    public String clientGrid() {
        return PREFIX + "/client_grid.html";
    }


    /**
     * 查询列表
     *

     * @Date 2020-11-02
     */
    @ResponseBody
    @RequestMapping("/clientGridList")
    public PageInfo clientGridList(ClientParam clientParam) {
        clientParam.setState(0);
        clientParam.setAudit(1);
        return this.clientService.findPageBySpec(clientParam);
    }

}


